<?php
namespace app\admin\model;
use app\common\model\WxappReg as WxappRegModel;
use app\common\model\Config as ConfigModel;
/**
 * 商户申请认证模型
 */
class WxappReg extends WxappRegModel
{
	/**
     * 编辑
     */
    public function edit($data)
    {
		//提交到微信审核
		if($data['status']==20){
			$result = $this->action();
			if($result['errcode']!=0){
				$this->error = '错误代码：'.$result['errcode'].'错误说明：'.$result['errmsg'];
				return false;
			}
			sand_examine_msg($this->wxapp_reg_id,'注册小程序 - 提交注册','待审核',$this->wxapp_id);
		}elseif($data['status']==40){
			sand_examine_msg($this->wxapp_reg_id,'注册信息被驳回','注册失败',$this->wxapp_id);
		}
		
        return $this->allowField(true)->save($data);
    }
	
	/**
     * 提交申请
     */
    private function action()
    {
		$config = ConfigModel::detail();	//获取第三方配置
		$url = 'https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=create&component_access_token='.$config['component_access_token'];
		$data ='{"name":"'.$this['legal_name'].'","code":"'.$this['code'].'","code_type":'.$this['code_type']['value'].',"legal_persona_wechat":"'.$this['legal_persona_wechat'].'","legal_persona_name":"'.$this['legal_persona_name'].'","component_phone":"'.$this['legal_persona_phone'].'"}';
		$result = json_decode(http_request($url,$data),true);
		return $result;
    }
	
	/**
     * 删除
     */
    public function remove()
    {
		if($this['status']['value']==30){
			$this->error = '该申请已注册成功，不允许删除';
            return false;
		}
        return $this->delete();
    }
	
	/**
     * 查询申请状态
     */
    public function search()
    {
		$config = ConfigModel::detail();	//获取第三方配置
		$url = 'https://api.weixin.qq.com/cgi-bin/component/fastregisterweapp?action=search&component_access_token='.$config['component_access_token'];
		$data ='{"name":"'.$this['legal_name'].'","legal_persona_wechat":"'.$this['legal_persona_wechat'].'","legal_persona_name":"'.$this['legal_persona_name'].'"}';
		return http_request($url,$data);
    }
	
}
